﻿package core.display  {
	
	import flash.display.MovieClip;
	import base.BaseMc;
	import core.display.RankItemMedalOne;
	import core.display.RankItemMedalTwo;
	import core.display.RankItemMedalThree;
	import core.display.RankItemMultiply;
	import core.display.RankItemFlower;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import  flash.display.Bitmap;

    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
	import core.display.RankItemModule;



	public class RankItem extends BaseMc  {

		private var rankItemMedalOne: RankItemMedalOne;
		private var rankItemMedalTwo: RankItemMedalTwo;
		private var rankItemMedalThree: RankItemMedalThree;
		private var rankItemMultiply: RankItemMultiply;
		private var rankItemFlower: RankItemFlower;
		private var rankField: TextField;
		private var nameField: TextField;
		private var flowerCountsField: TextField;
		private var itemModule:Object;
		private var loader:Loader;
		private var loaderDefault:Loader;
		private var defaultAvaterUrl:String = "http://hbm.jyq365.com/images/flash/avatar1.png";

		public function RankItem() {

		}
		
		//当影片放到场景上后的初始化方法
		protected override function onInit():void {
			this.initCompents();
		}
		
		//当影片从场景上移除的时候周期方法 
		protected override function onRemove():void {
			
		}
		
		
		private function initCompents():void {
			
			rankItemMedalOne = new RankItemMedalOne();
			rankItemMedalOne.x =30;
			rankItemMedalOne.y = 0;
			this.addChild(rankItemMedalOne);
			
			rankItemMedalTwo = new RankItemMedalTwo();
			rankItemMedalTwo.x =30;
			rankItemMedalTwo.y = 0;
			this.addChild(rankItemMedalTwo);
			
			rankItemMedalThree = new RankItemMedalThree();
			rankItemMedalThree.x =30;
			rankItemMedalThree.y = 0;
			this.addChild(rankItemMedalThree);
			
			var textformat: TextFormat = new TextFormat();
			textformat.size = 35 ;
			var flowerTextformat: TextFormat = new TextFormat();
			flowerTextformat.bold = true;
			flowerTextformat.size = 38 ;
			
			rankField = new TextField();
			rankField.defaultTextFormat=textformat;
			rankField.textColor =0x653e44;
			rankField.autoSize=TextFieldAutoSize.LEFT;
			rankField.width = 50;
			rankField.height = 50;
			rankField.x = 40;
			rankField.y = 10;
			this.addChild(rankField);
						
			nameField = new TextField();
			nameField.defaultTextFormat=textformat;
			nameField.textColor =0x653e44;
			nameField.autoSize=TextFieldAutoSize.LEFT;
			nameField.width = 200;
			nameField.height = 50;
			nameField.x = 160;
			nameField.y = 10;
			this.addChild(nameField);
			
			rankItemFlower = new RankItemFlower();
			rankItemFlower.x =560;
			rankItemFlower.y = 15;
			this.addChild(rankItemFlower);
			
			rankItemMultiply = new RankItemMultiply();
			rankItemMultiply.x =620;
			rankItemMultiply.y = 27;
			this.addChild(rankItemMultiply);
			
			flowerCountsField = new TextField();
			flowerCountsField.defaultTextFormat=flowerTextformat;
			flowerCountsField.textColor =0x653e44;
			flowerCountsField.autoSize=TextFieldAutoSize.LEFT;
			flowerCountsField.width = 60;
			flowerCountsField.height = 50;
			flowerCountsField.x = 650;
			flowerCountsField.y = 10;
			this.addChild(flowerCountsField);
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		
		public function setItemData(itemModule:Object):void {
			this.itemModule = itemModule;
			
			rankField.text=itemModule.rank;
			nameField.text= itemModule.name;
			flowerCountsField.text=itemModule.red;
			
			if(itemModule.rank){
				var rankInt:int = parseInt(itemModule.rank);
				if(rankInt == 1){
				rankField.visible=false;
				rankItemMedalOne.visible=true;
				rankItemMedalTwo.visible=false;
				rankItemMedalThree.visible =false;
				}
				
				if(rankInt == 2){
				rankField.visible=false;
				rankItemMedalOne.visible=false;
				rankItemMedalTwo.visible=true;
				rankItemMedalThree.visible =false;
				}
				
				if(rankInt == 3){
				rankField.visible=false;
				rankItemMedalOne.visible=false;
				rankItemMedalTwo.visible=false;
				rankItemMedalThree.visible =true;
				}
				
				if(rankInt > 3){
				rankField.visible=true;
				rankItemMedalOne.visible=false;
				rankItemMedalTwo.visible=false;
				rankItemMedalThree.visible =false;
				}
			}
			
			loadHeaderImage(this.itemModule.avatar);
		}
		
		
		 private function loadHeaderImage( url : String):void {
            var request:URLRequest = new URLRequest(url);
            loader.load(request);
        }

		/**
		*生成头像View
		*/
        private function createImageView(original:Bitmap):Bitmap {
            var babyHeaderImageView:Bitmap = new Bitmap(original.bitmapData.clone());
            var mask : MovieClip = createImageMask();
			babyHeaderImageView.x = 90;
			babyHeaderImageView.y = 10;
			babyHeaderImageView.width=50;
			babyHeaderImageView.height=50;
			babyHeaderImageView.mask = mask;
            this.addChild(babyHeaderImageView);
			this.addChild(mask);
            return babyHeaderImageView;
        }
		
		private function createImageMask(): MovieClip{
			var mask:MovieClip = new MovieClip();
			mask.graphics.beginFill(0x000000,1);
			mask.graphics.drawRoundRect(90,10,50,50,10,10);
			//mask.graphics.drawCircle(0,0,25);
			mask.graphics.endFill();
			//mask.width = 50;
			//mask.height = 50;
			//mask.x = 115;
			//mask.y = 35;
			return mask;
			
		}

        private function completeHandler(event:Event):void {
            var loader:Loader = Loader(event.target.loader);
            var image:Bitmap = Bitmap(loader.content);
            createImageView(image);
        }
        
        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("Unable to load  image ");
			loadDefaultHeaderImage(this.defaultAvaterUrl);
        }
		
		
		private function loadDefaultHeaderImage(url:String):void{
			if(loaderDefault == null){
				loaderDefault = new Loader();
				loaderDefault.contentLoaderInfo.addEventListener(Event.COMPLETE, completeDefaultHandler);
            	loaderDefault.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioDefaultErrorHandler);
				var req:URLRequest = new URLRequest(url);
				loaderDefault.load(req);
			}else{
            	var image:Bitmap = Bitmap(loaderDefault.content);
            	createImageView(image);
			}
		}
		
		private function completeDefaultHandler(e:Event):void {
			var image:Bitmap = Bitmap(loaderDefault.content);
            createImageView(image);
		}
		
		private function ioDefaultErrorHandler(e:IOErrorEvent):void {
			trace("ioDefaultErrorHandler");
		}


	}
	
}
